package com.shanzmoo.stm.admin.web;

import cn.hutool.core.util.ObjectUtil;
import com.shanzmoo.base.bean.SHttpResponse;
import com.shanzmoo.base.constant.Constant;
import com.shanzmoo.core.util.BCryptUtils;
import com.shanzmoo.db.store.entity.StoreMemberEntity;
import com.shanzmoo.db.store.service.IStoreMemberService;
import com.shanzmoo.stm.admin.domain.dto.StoreMemberUpdatePasswordDTO;
import com.shanzmoo.stm.admin.util.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 仓储用户系统用户 控制层
 *
 * @author： Gzhao 2021/3/14
 */
@Api(tags = "系统用户")
@RestController
@RequestMapping("/sys/user")
public class StmSysUserController {
    /**
     * 仓储用户初始密码
     */
    private static final String INIT_PASSWORD = "777777";

    @Autowired
    private IStoreMemberService storeMemberService;

    @ApiOperation(value = "更新密码")
    @PostMapping("/update-password")
    public SHttpResponse<Boolean> updatePassword(@Valid @RequestBody StoreMemberUpdatePasswordDTO updateDto) {
        StoreMemberEntity member = storeMemberService.getById(SecurityUtil.getCurrentUserId());
        if (ObjectUtil.isNull(member)) {
            return SHttpResponse.fail(Constant.ERRCODE_LOGIC, "未获取到用户信息");
        }
        member.setPassword(BCryptUtils.encodePassword(updateDto.getPassword()));

        storeMemberService.updateById(member);
        return SHttpResponse.ok(Boolean.TRUE);
    }

 }
